<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Cart extends Model
{
    /**
     * 定义主键字段
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 定义表名，不含前缀
     *
     * @var string
     */
    protected $table      = 'wd_user_cart';

    /**
     * 定义拒绝保存的字段
     *
     * @var array
     */
    protected $guarded    = ['id'];

    /**
     * 开启laravel默认的时间记录
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 逆向关联用户表
     *
     * @return void
     */
    public function user()
    {
        return $this->belongsTo('App\Models\User', 'user_id', 'id');
    }

    /**
     * 逆向关联商品表
     *
     * @return void
     */
    public function goods()
    {
        return $this->belongsTo('App\Models\Goods', 'goods_id', 'id');
    }

    /**
     * 逆向关联商品规格表
     *
     * @return void
     */
    public function norm()
    {
        return $this->belongsTo('App\Models\GoodsNorm', 'goods_norm_id', 'id');
    }

    /**
     * 修改返回的购物车数据
     *
     * @param Cart $dat
     * @return void
     */
    public static function show_cart( $data)
    {
        foreach ($data as $k => $v) {
            $v['good_name'] = $v['goods']['name'];
            $v['good_price'] = $v['goods']['retail_price'];
            $v['good_pic'] = $v['goods']['pic'];
            $v['good_norm'] = $v['norm']['name'];
            
            unset($v['goods']);
            unset($v['norm']);
        }
        
        return $data;
    }

}
